﻿<%@ WebHandler Language="C#" Class="tencent_geocoder" %>

using System;
using System.Web;
using Common.Helper;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using System.IO;

public class tencent_geocoder : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //https://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=DEOBZ-TWRCG-O5UQU-IWJ3P-JH3QK-MJFRU&get_poi=1
        //https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder


        //https://lbs.qq.com/service/webService/webServiceGuide/webServiceSearch
        //https://apis.map.qq.com/ws/place/v1/search?boundary=region(全国)&keyword=博物馆&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77

        try
        {


            string url = "https://apis.map.qq.com/ws/geocoder/v1/";
            //location: getLat + "," + getLng,
            //key: "DEOBZ-TWRCG-O5UQU-IWJ3P-JH3QK-MJFRU",
            //get_poi: 1

            Dictionary<string, string> PramaryArry = new Dictionary<string, string>();
            PramaryArry.Add("location", context.Request["location"]);
            PramaryArry.Add("key", "DEOBZ-TWRCG-O5UQU-IWJ3P-JH3QK-MJFRU");
            PramaryArry.Add("get_poi", "0");

            context.Response.Write(Get(url, PramaryArry));
        }
        catch (Exception e)
        {
            context.Response.Write(e);
        }
    }


    /// <summary>
    /// 发送Get请求
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="dic">请求参数定义</param>
    /// <returns></returns>
    private static string Get(string url, Dictionary<string, string> dic)
    {
        string result = "";
        StringBuilder builder = new StringBuilder();
        builder.Append(url);
        if (dic.Count > 0)
        {
            builder.Append("?");
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
        }
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
        //添加参数
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        Stream stream = resp.GetResponseStream();
        try
        {
            //获取内容
            using (StreamReader reader = new StreamReader(stream))
            {
                result = reader.ReadToEnd();
            }
        }
        finally
        {
            stream.Close();
        }
        return result;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}